﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ColumnDefinition column1CloneForLayer0;
        bool editMode = false;
        bool addWidgetMode = false;
        string currSelectedShaderName = "N/A";

        internal bool IsAddWidgetMode
        {
            get { return addWidgetMode; }
        }

        internal string ReadyToAddShaderName
        {
            get { return currSelectedShaderName; }
            set { currSelectedShaderName = value; }
        }

        internal bool IsEditMode
        {
            get { return editMode; }
        }

        public Window1()
        {
            InitializeComponent();

            column1CloneForLayer0 = new ColumnDefinition();
            column1CloneForLayer0.SharedSizeGroup = "column1";

            //-------------------------
            initApp();
        }

        public void panel1Button_MouseEnter(object sender, MouseEventArgs e)
        {
            layer1.Visibility = Visibility.Visible;

            Grid.SetZIndex(layer1, 1);
        }

        public void layer0_MouseEnter(object sender, MouseEventArgs e)
        {
            if (panel1Button.Visibility == Visibility.Visible)
                layer1.Visibility = Visibility.Collapsed;
        }

        public void panel1Pin_Click(object sender, RoutedEventArgs e)
        {
            if (panel1Button.Visibility == Visibility.Collapsed)
                UndockPanel();
            else
                DockPanel();
        }

        public void DockPanel()
        {
            panel1Button.Visibility = Visibility.Collapsed;
            panel1PinImage.Source = new BitmapImage(new Uri("D:\\Source\\wpfTool\\wpfTool\\image\\pin.gif"));

            layer0.ColumnDefinitions.Add(column1CloneForLayer0);
        }

        public void UndockPanel()
        {
            layer1.Visibility = Visibility.Visible;
            panel1Button.Visibility = Visibility.Visible;
            panel1PinImage.Source = new BitmapImage(new Uri("D:\\Source\\wpfTool\\wpfTool\\image\\pinHorizontal.gif"));

            layer0.ColumnDefinitions.Remove(column1CloneForLayer0);
        }

        void initApp()
        {
            shaderMgr = new ShaderManager(this);
            propertyGridMgr = new PropertyGridManager(this);

            pipelineViewMgr = new PipelineViewManager(this);

            thePreviewModeToggleButton.Click += new RoutedEventHandler(shaderMgr.PreviewMode_ToggleButton_Click);
        }


        internal ShaderManager shaderMgr = null;
        internal PipelineViewManager pipelineViewMgr = null;
        internal PropertyGridManager propertyGridMgr = null;

        // all delegate are here
        public delegate void ShaderUpdatedHandler(System.Drawing.Bitmap bmp);
        public delegate void ShaderProgressEventHandler(float progress);
        public delegate void CreateShaderEventHandler(PipelineView pv, string shaderName, out ShaderBase shader);
        public delegate void PipelineStructureChangeHandler(PipelineView pv, ShaderBase src, int srcPort, ShaderBase dst, int dstPort);
        public delegate void PropertyGridItemChangeHandler(FrameworkElement fe, object value);
        public delegate void ShaderPropertyChangeHandler(ShaderBase shader);
        public delegate void ShaderWidgetMoveHandler(ShaderWidget wgt);

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            editMode = !editMode;
        }

        private void MenuItem_Run_Click(object sender, RoutedEventArgs e)
        {
            shaderMgr.updatePipeline(pipelineViewMgr.CurrentPipeline);
        }

        private void MenuItem_Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MenuItem_AddPipeline_Click(object sender, RoutedEventArgs e)
        {
            pipelineViewMgr.addPipelineView();
        }

        private void ToggleButton_AddWidget_Click(object sender, RoutedEventArgs e)
        {
            addWidgetMode = !addWidgetMode;

            if (IsAddWidgetMode)
            {
                this.editMode = false;
                EditButton.IsEnabled = false;
                theTab.Cursor = Cursors.Cross;
            }
            else
            {
                EditButton.IsChecked = false;
                EditButton.IsEnabled = true;
                theTab.Cursor = Cursors.Arrow;
            }
        }

    }
}
